package com.demo.xj_plane.common.utils;

import lombok.Data;

/**
 * 分页算法类
 */
@Data
public class PageCount {
    protected static final int DEFAULT_START = 1;
    protected static final int DEFAULT_LIMIT = 10;

    protected Integer start;
    protected Integer limit;
    private Integer totalCount;

    public PageCount() {
        this(DEFAULT_START, DEFAULT_LIMIT);
    }

    public PageCount(int pageNo) {
        this(pageNo, DEFAULT_LIMIT);
    }

    public PageCount(int pageNo, int pageRow) {
        pageNo = pageNo <= 0 ? 1 : pageNo;
        pageRow = pageRow < 0 ? 0 : pageRow;
        this.start = (pageNo - 1) * pageRow;
        this.limit = pageRow;
    }

    public int getTotalPages() {
        if (totalCount == 0){
            return 0;
        }
        int count = totalCount / limit;
        if (totalCount % limit > 0) {
            count++;
        }
        return count;
    }
}
